import Utils from '../utils/Common'
import { GetAllFlowNodeDetailInfoDebounce } from '@/api/flow/flow'
import { AppParams } from '@/utils/appParams'
import { Emitter } from "@/utils/Emitter";
import {useFlowStore} from "@/store/modules/flow/flowStore";

/**
 * 混入JS，在节点的配置组件内混入，组件内的程序会自动混入一下 属性 、 方法 和钩子
 *
 * 使用方法 ，在组件内导入,在 export default 内 添加,即可：
 * 如下：
 *
 * import NodeConfigMixin from '@/libs/NodeConfigMixin'
 * mixins: [NodeConfigMixin],
 *
 * @type {{data(): {currentClickNodeMixin: null}, mounted(): void}}
 */
const NodeConfigMixin = {
  data () {
    return {
      currentClickNodeMixin: null, // 弃用，使用 useFlowStore （路径：import {useFlowStore} from "@/store/modules/flow/flowStore";）下的 CurrentClickNode 替代
      currentClickNodeJsonMixin: null,
      nodeInputParamsMixin: [],
      flowListDetailDataMixin: [],
      isGetAllFlowNode: false,
      GlobalParams: []
    }
  },
  methods: {
    SetNodeInputParams () {
      if (Utils.IsEmpty(this.currentClickNodeMixin) || Utils.IsEmpty(this.currentClickNodeMixin.id)) {
        return
      }
      const FlowStore = useFlowStore();
      const target = Utils.traverse(this.currentClickNodeMixin, [])
      const GlobalParams = FlowStore.GlobalParams
      this.GlobalParams = GlobalParams
      if (GlobalParams) {
        const _Key = AppParams.DATA_SOURCE_TYPE.GLOBAL_PARAM
        for (const gp of GlobalParams) {
          const _Keys = [gp.Id, _Key, gp.Name]
          target.push({
            DataSource: _Key,
            ParamName: gp.Name,
            nodeId: gp.Id,
            Value: gp.Value,
            ValueType: gp.Type,
            key: _Keys.join('_')
          })
        }
      }
      this.nodeInputParamsMixin = target
      GetAllFlowNodeDetailInfoDebounce((res) => {
        this.flowListDetailDataMixin = res
      })
    },
    InitNodeConfigMixin () {
      this.currentClickNodeMixin = this.$store.getters.currentClickNode
      this.SetNodeInputParams()
    }
  },
  created () {
    this.InitNodeConfigMixin()
    Emitter.on(AppParams.JAVA_SCRIPT_EVENT.NODE_CLICK, (v) => {
      this.InitNodeConfigMixin()
    })
  },
  onMounted () {
    this.InitNodeConfigMixin()
  }
}

export default NodeConfigMixin
